package controllers

import (
	"057dg/models"
	// "fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/cache"
	"github.com/astaxie/beego/utils/captcha"
	"strconv"
)

var cpt *captcha.Captcha

func init() {
	// use beego cache system store the captcha data
	store := cache.NewMemoryCache()
	cpt = captcha.NewWithFilter("/captcha/", store)
	cpt.StdWidth = 80
	cpt.StdHeight = 40
	cpt.ChallengeNums = 4
}

type HomeController struct {
	beego.Controller
}

//主页
func (this *HomeController) Get() {
	this.Layout = "layout/layout.html"
	this.Data["Website"] = "beego.me"
	this.Data["Email"] = "astaxie@gmail.com"
	this.Data["IsLogin"] = checkAccount(this.Ctx)
	this.TplNames = "home/index.html"
}

//登录
func (this *HomeController) Login() {
	dosubmit := this.GetString("dosubmit")
	if dosubmit == "true" {
		email := this.GetString("email")
		pwd := this.GetString("password")
		autologin := this.GetString("autologin") == "on"

		u, err := models.GetUserByEmail(email)
		if err != nil {
			this.Data["notice"] = "用户名或密码错误"
		} else {
			if u.Email == email && u.PassWord == GetMd5String(pwd) {
				maxExpre := 60 * 60 * 6
				if autologin == true {
					//maxExpre = 1<<31 - 1
					maxExpre = 60 * 60 * 6
				}
				uid := strconv.Itoa(u.Id)

				this.Ctx.SetCookie("uid", uid, maxExpre, "/")
				this.Ctx.SetCookie("email", email, maxExpre, "/")
				this.Ctx.SetCookie("password", GetMd5String(pwd), maxExpre, "/")
				if u.RType > 2 {
					this.Redirect("/acompany/company", 301)
				}
				this.Redirect("/account", 301)
			}
		}
	}
	this.TplNames = "home/login.html"
}

//注册
func (this *HomeController) Register() {
	dosubmit := this.GetString("dosubmit")
	if dosubmit == "true" {
		resultmap, _ := InsertData(this)
		this.Data["json"] = resultmap

		if rType, err := this.GetInt("rType"); err == nil {
			if rType == 2 {
				//新增一条简历
				resume := new(models.Resume)
				if uid, err := strconv.Atoi(resultmap["uid"]); err == nil {
					resume.CreateByUid = uid
					models.AddResume(resume)
				}

			} else {
				// //新增一条公司记录
				company := new(models.Company)
				if uid, err := strconv.Atoi(resultmap["uid"]); err == nil {
					company.CreateByUid = uid
					models.AddCompany(company)
				}
			}
		}
		this.ServeJson()
		return
	}
	this.TplNames = "home/register.html"
}

//激活页面
func (this *HomeController) Active() {
	this.Data["email"] = this.GetString("email")
	this.TplNames = "home/active.html"
}

//邮箱检测
func (this *HomeController) CheckEmail() {
	//json := json.Unmarshal(this.Ctx.Input.RequestBody, &maps)
	// types := this.GetString("type")
	email := this.GetString("email")
	_, err := models.GetUserByEmail(email)
	if err != nil {
		this.Data["json"] = 200
	} else {
		this.Data["json"] = "此邮箱已注册"
	}
	this.ServeJson()
}

//入库操作
func InsertData(this *HomeController) (mapjson map[string]string, err error) {
	mapjson = map[string]string{}
	mapjson["STATE"] = ""
	mapjson["errorType"] = ""
	mapjson["msg"] = ""
	//验证码校验
	captcha_id, code := this.GetString("captcha_id"), this.GetString("code")
	//this.Ctx.WriteString(strconv.FormatBool(b))
	if b := cpt.Verify(captcha_id, code); b != true {
		mapjson["errorType"] = "202"
		mapjson["errorType"] = "code"
		mapjson["msg"] = "验证码不正确"
	}

	//入库操作
	user := new(models.User)
	user.Email = this.GetString("email")
	user.PassWord = GetMd5String(this.GetString("pwd"))
	user.RType, _ = this.GetInt("rType")

	if uid, err := models.AddUser(user); err == nil {
		mapjson["STATE"] = "200"
		mapjson["uid"] = strconv.FormatInt(uid, 10)
	} else {
		mapjson["errorType"] = ""
		mapjson["msg"] = "注册失败"
	}
	return
}
